In-class Exercise 5

A short description of the post.

Nor Aisyah https://www.linkedin.com/in/nor-aisyah/
09-13-2021

To show your figure images in a higher resolution, fix the retina in the above code chunk above.

1. Install and load R packages

packages = c('maptools', 'sf', 'raster', 'spatstat', 'tmap', 'tidyverse')
for (p in packages){
  if(!require(p, character.only = T)){
    install.packages(p)
    }
  library(p,character.only = T)
}

More on the packages used:

2. Data used:

3. Import the Geospatial data

mpsz_sf <- st_read(dsn = "data/shapefile", layer = "MP14_SUBZONE_WEB_PL")
Reading layer `MP14_SUBZONE_WEB_PL' from data source 
  `C:\aisyahajit2018\IS415\IS415_blog\_posts\2021-09-13-in-class-exercise-5\data\shapefile' 
  using driver `ESRI Shapefile'
Simple feature collection with 323 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
Projected CRS: SVY21

We can see from the results above that mpsz_sf projection is in SVY21.

4. Import aspatial data from rds folder

CHAS <- read_rds("data/rds/CHAS.rds")
childcare <- read_rds("data/rds/childcare.rds")

Note: There are some data issue in the childcare data frame because Lat and Lng should be in numeric data type. The coordinate fields are in decimal degrees. Hence, wgs referencing system is assumed.

5. Convert aspatial data frame into sf objects

5.1 CHAS conversion to sf object

CHAS_sf <- st_as_sf(CHAS,
                    coords = c("X_COORDINATE",
                               "Y_COORDINATE"),
                    crs=3414)

Note: st_as_sf accepts the coordinates in character data type.

5.2 Childcare conversion to sf object

childcare_sf <- st_as_sf(CHAS,
                    coords = c("Lng",
                               "Lat"),
                    crs=4326) %>%
  st_transform(crs=3414)

Note: In childcare we have Lng and Lat as the coordinates where they are both in demical degrees. The coordinate system for that is WGS84, crs code 4326. We then use st_transform to change the coordinates of a geometry from one spatial reference system to another.

6. Plot to review

Notes for arguments: - alpha is to set transparency - col is to set the colour of the dots - size is to set the size of the dots

tmap_mode('view')
tm_shape(childcare_sf) +
  tm_dots(alpha=0.4,
          col="blue",
          size=0.05) +
tm_shape(CHAS_sf) +
  tm_dots(alpha=0.4,
          col="red",
          size=0.05)
tmap_mode('plot')

Note:

7. Geospatial Data Wrangling

7.1 Converting from sf to Spatial* classes

The asterisk in Spatial* is to say that we have more than 1 Spatial objects

7.2 Converting from Spatial* classes to sp format

7.3 Converting from sp format to spatstat ppp format